<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="robots" content="noindex, nofollow">
    <title>Desktop Metrics logger with Google Analytics</title>
    <script>
        const SAVED_GOOGLE_ANALYTICS_DESKTOP_KEY = "SAVED_GOOGLE_ANALYTICS_DESKTOP_KEY";
        let gaReady = false, loadedGoogleAnalyticsID;

        const url = window.location.href;
        const urlParams = new URLSearchParams(window.location.search);
        const reloadedOnce = urlParams.get('reloadedOnce');

        /**
         * GA doesn't log engagement metrics if the page is in focus. Since we are a hidden page, we will take over
         * the global event handlers and trigger synthetic focus events to make ga think that it has focus.
         */
        function patchGlobalFocusHandlers() {
            let gaFocusCallback, focusCallbackDone;
            window.savedEventListener = window.addEventListener;
            savedEventListener('focus', (focusEvent)=>{
                gaFocusCallback.call(this, focusEvent);
                focusCallbackDone = true;
            });

            window.addEventListener = function (eventName, fn, globalEvFlag) {
                if(eventName === 'focus'){
                    const focusEvent = new Event('focus', {
                        view: window,
                        bubbles: true,
                        cancelable: true,
                        isTrusted: true,
                        cancelBubble: false,
                        composed : false,
                        defaultPrevented: false,
                        returnValue: true
                    });
                    document.body.focus();
                    setTimeout(()=>{
                        gaFocusCallback = fn;
                        // first lets try to trigger the actual saved savedEventListener programmatically
                        window.dispatchEvent(focusEvent);
                    }, 500);
                    setTimeout(()=>{
                        // the event wasnt generated, so we trigger it manually. This is not preferred as the target and
                        // other important properties of the event are readonly and cannot be overwritten.
                        if(!focusCallbackDone){
                            gaFocusCallback.call(window.document.body, focusEvent);
                        }
                    }, 1000);
                } else if(eventName === 'blur'){
                    // swallow this event as ga will not track engagement if not focused.
                } else {
                    window.savedEventListener(eventName, fn, globalEvFlag);
                }
            }
        }
        patchGlobalFocusHandlers();

        function installGoogleAnalytics() {
            if(gaReady){
                return ;
            }
            const analyticsID = localStorage.getItem(SAVED_GOOGLE_ANALYTICS_DESKTOP_KEY);
            if(!analyticsID) {
                console.error("No analytics id found to init ga, waiting for phcode to send ids to init...");
                return;
            }
            loadedGoogleAnalyticsID = analyticsID;
            // ga primer scripts
            window.dataLayer = window.dataLayer || [];
            window.gtag = function() {
                window.dataLayer.push(arguments);
                if(window.dataLayer.length > 500){
                    window.dataLayer.splice(0, 250); // remove half the elements offline queue guard
                }
            }

            const gaScript = document.createElement('script');
            gaScript.async = true;
            gaScript.src = `https://www.googletagmanager.com/gtag/js?id=${analyticsID}`;
            document.head.appendChild(gaScript);

            gtag('js', new Date());
            gtag('config', analyticsID);
            gaReady = true;
        }

        installGoogleAnalytics();

        function reloadOnce() {
            // we have to do this as ga seems to miss points in real time analytics dashboard if ga is not inited
            // at page load time. So we reload once to make metrics show up in ga real time dashboard.
            let currentUrl = new URL(window.location);
            if (!currentUrl.searchParams.has('reloadedOnce')) {
                currentUrl.searchParams.set('reloadedOnce', 'true');
                window.location.href = currentUrl.href;
            }
        }


        async function processRequest(event) {
            const payload = event.payload;
            if(payload.analyticsID && loadedGoogleAnalyticsID !== payload.analyticsID) {
                localStorage.setItem(SAVED_GOOGLE_ANALYTICS_DESKTOP_KEY, payload.analyticsID);
                installGoogleAnalytics();
            }

            for(let eventPayload of payload.events) {
                gtag('event', eventPayload.eventAct, {
                    'event_category': eventPayload.category,
                    'event_label': eventPayload.label,
                    'value': eventPayload.count
                });
            }
            if(!reloadedOnce) {
                setTimeout(reloadOnce, 1000);
            }
        }
        window.__TAURI__.event.listen("health", processRequest);
        setInterval(async ()=>{
            // close window if the metrics hidden window and file drop window is the only one around.
            const allTauriWindowsLabels  = await window.__TAURI__.invoke('_get_window_labels');
            if(allTauriWindowsLabels.length === 2 || allTauriWindowsLabels.length === 1){
                window.__TAURI__.window.getCurrent().close();
            }
        }, 1000);
    </script>
</head>
<body>
Phoenix Desktop Metrics emitter to GA.
</body>
</html>